const app = getApp()
const constant = require("../../../../../constant.js")
const storage = require("../../../../../utils/storage.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    deleteCard: false,
    cardListContent: false,
    disabled: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      host: constant.api(app.globalData.pord).url,
      qiniu: constant.resource.qiniu,
      mobile: wx.getStorageSync(storage.keys.mobile),
      courseRoundId: options.courseRoundId,
      courseScheduleId: options.courseScheduleId
    })

    if(options.from) {
      this.setData({
        from: options.from
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.request();
  },

  request: function () {
    wx.showLoading({
      title: "正在加载中..."
    })

    var that = this;

    wx.request({
      url: that.data.host + '/coach/course-rounds/' + that.data.courseRoundId + '/schedule-coursewares/' + that.data.courseScheduleId +
        '?mobile=' + that.data.mobile,
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        if (res.data.code == "0") {
          that.setData({
            data: res.data.data
          });

          wx.hideLoading();
        } else {
          wx.hideLoading();

          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 1000
          })
        }
      },
      fail: function (res) {
        wx.hideLoading();

        wx.showToast({
          title: '请求数据失败',
          icon: 'none',
          duration: 1000
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  checkboxChange: function(e) {
    console.log(e.detail.value.length)
    this.setData({
      checkedList: e.detail.value.length > 0 ? true : false
    })
  },

  bindClickDelete: function() {
    this.setData({
      deleteCard: !this.data.deleteCard
    })
  },

  formSubmit: function (e) {
    var that = this;

    if (that.data.deleteCard) {
      if (e.detail.value.coursewareIds.length == 0) {
        wx.showToast({
          title: '请选择要删除的动作',
          icon: 'none',
          duration: 1000
        });
        return;
      } else {
        that.setData({
          disabled: true
        });

        wx.request({
          url: that.data.host + '/coach/course-rounds/' + that.data.courseRoundId + '/schedule-coursewares/' + that.data.courseScheduleId + '/delete-courseware?mobile=' + that.data.mobile,
          method: 'POST',
          data: e.detail.value.coursewareIds,
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            if (res.data.code == "0") {
              wx.showToast({
                title: res.data.message,
                icon: 'success',
                duration: 1000,
                mask: true,
                success: function () {
                  setTimeout(function () {
                    that.request();
                    that.setData({
                      deleteCard: false,
                      disabled: false
                    });
                  }, 1000)
                }
              });
            } else {
              wx.showToast({
                title: res.data.message,
                icon: 'none',
                duration: 1000,
                mask: true
              })
              that.setData({
                disabled: false
              });
            }
          },
          fail: function (res) {
            wx.showToast({
              title: '删除失败',
              icon: 'none',
              duration: 1000,
              mask: true
            })
            that.setData({
              disabled: false
            });
          }
        });
      }
    } else {
      var flag = true;
      for (let i in that.data.data.coursewares) {
        if (that.data.data.coursewares[i].group_amount === 0 || that.data.data.coursewares[i].group_amount === "0" || !that.data.data.coursewares[i].group_amount) {
          flag = false;
          wx.showToast({
            title: '组数不能为零或空',
            icon: 'none',
            duration: 1000,
            mask: true
          })
          return;
        } else if (that.data.data.coursewares[i].movement_number === 0 || that.data.data.coursewares[i].movement_number === "0" || !that.data.data.coursewares[i].movement_number) {
          flag = false;
          wx.showToast({
            title: '每组次数不能为零或空',
            icon: 'none',
            duration: 1000,
            mask: true
          })
          return;
        }
      }

      if(flag) {
        that.setData({
          disabled: true
        });

        wx.request({
          url: that.data.host + '/coach/course-rounds/' + that.data.courseRoundId + '/schedule-coursewares/' + that.data.courseScheduleId + '/edit-courseware?mobile=' + that.data.mobile,
          method: 'POST',
          data: that.data.data.coursewares,
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            if (res.data.code == "0") {
              wx.showToast({
                title: res.data.message,
                icon: 'success',
                duration: 1000,
                mask: true,
                success: function () {
                  setTimeout(function () {
                    that.request();
                    that.setData({
                      disabled: false
                    });
                  }, 1000)
                }
              });
            } else {
              wx.showToast({
                title: res.data.message,
                icon: 'none',
                duration: 1000,
                mask: true
              })
              that.setData({
                disabled: false
              });
            }
          },
          fail: function (res) {
            wx.showToast({
              title: '保存失败',
              icon: 'none',
              duration: 1000,
              mask: true
            })
            that.setData({
              disabled: false
            });
          }
        });
      }
    }
  },

  inputGroupAmount: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;

    that.data.data.coursewares[index].group_amount = e.detail.value ? e.detail.value : '';
    that.setData({
      coursewares: that.data.data.coursewares
    })
  },

  inputMovementAmount: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;

    that.data.data.coursewares[index].movement_number = e.detail.value ? e.detail.value : '';
    that.setData({
      coursewares: that.data.data.coursewares
    })
  },

  inputMaxWeight: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;

    that.data.data.coursewares[index].max_weight = e.detail.value ? e.detail.value : ''
    that.setData({
      coursewares: that.data.data.coursewares
    })
  },

  inputTime: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;

    that.data.data.coursewares[index].time = e.detail.value ? e.detail.value : '';
    that.setData({
      coursewares: that.data.data.coursewares
    })
  }
})